﻿using Admin.NET.Core.Btms;

namespace Admin.NET.Application.Service;

public partial class TraPeiXueService
{
    public async Task<List<TraPeiXueMingXiOutput>> GetZiBiao(string id)
    {
        var TraShenQingZiBiaoList = await _traShenQingPeiXueZiBiao
            .AsQueryable()
            .Where(a => a.traPeiXueId == id)
            .Select<TraPeiXueMingXiOutput>()
            .ToListAsync();
        foreach (var item in TraShenQingZiBiaoList)
        {
            item.xueYe = await _invKuCunBiao.GetByIdAsync(item.xueYeId);
        }
        return TraShenQingZiBiaoList;
    }

    /// <summary>
    /// 新增 申请子表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    private void AddWithZiBiaoInput(List<TraPeiXueMingXiInput> inputList, string id)
    {
        // 插入明细表
        List<TraPeiXueMingXi> listZiBiao = new List<TraPeiXueMingXi>();
        foreach (var item in inputList)
        {
            item.Id = null;
            item.traPeiXueId = id;
            listZiBiao.Add(item.Adapt<TraPeiXueMingXi>());
        }
        if (listZiBiao.Count() > 0)
        {
            _traShenQingPeiXueZiBiao.BulkCopy(listZiBiao);
        }
    }

    /// <summary>
    /// 更新 申请子表
    /// </summary>
    /// <param name="inputList"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    private async void UpdateWithZiBiaoInput(List<TraPeiXueMingXiInput> inputList, string id)
    {
        await _traShenQingPeiXueZiBiao.DeleteAsync(d => d.traPeiXueId == id);
        // 插入明细表
        List<TraPeiXueMingXi> listZiBiao = new List<TraPeiXueMingXi>();
        foreach (var item in inputList)
        {
            item.Id = null;
            item.traPeiXueId = id;
            listZiBiao.Add(item.Adapt<TraPeiXueMingXi>());
        }
        if (listZiBiao.Count() > 0)
        {
            _traShenQingPeiXueZiBiao.BulkCopy(listZiBiao);
        }
    }
}
